package com.ai.trickycards.widgets

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.use
import com.ai.trickycards.R

/**
 * 自定义进度条
 */
class CustomProgressView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var backgroundColor = -1
    private var backgroundStartColor = -1
    private var backgroundEndColor = -1
    private var progressColor = -1
    private var progressStartColor = -1
    private var progressEndColor = -1
    private var radius = 0f
    var progress = 0f
        set(value) {
            field = value
            invalidate()
        }
    private var backgroundShader: LinearGradient? = null
    private val paint = Paint()
    private val rect = RectF()

    init {
        context.obtainStyledAttributes(attrs, R.styleable.CustomProgressView).use {
            backgroundColor = it.getColor(R.styleable.CustomProgressView_cpvBackgroundColor, backgroundColor)
            backgroundStartColor = it.getColor(R.styleable.CustomProgressView_cpvBackgroundStartColor, backgroundStartColor)
            backgroundEndColor = it.getColor(R.styleable.CustomProgressView_cpvBackgroundEndColor, backgroundEndColor)

            progressColor = it.getColor(R.styleable.CustomProgressView_cpvProgressColor, progressColor)
            progressStartColor = it.getColor(R.styleable.CustomProgressView_cpvProgressStartColor, progressStartColor)
            progressEndColor = it.getColor(R.styleable.CustomProgressView_cpvProgressEndColor, progressEndColor)

            radius = it.getDimensionPixelOffset(R.styleable.CustomProgressView_cpvRadius, 0).toFloat()
            progress = it.getFloat(R.styleable.CustomProgressView_cpvProgress, progress)
        }
        paint.isAntiAlias = true
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        backgroundShader = createLinearGradient(width.toFloat(), backgroundStartColor, backgroundEndColor, backgroundColor)
    }

    private fun createLinearGradient(
        width: Float,
        startColor: Int,
        endColor: Int,
        defaultColor: Int
    ): LinearGradient {
        var drawStartColor = startColor
        var drawEndColor = endColor
        if (startColor == -1 || endColor == -1) {
            drawStartColor = defaultColor
            drawEndColor = defaultColor
        }

        return LinearGradient(0f, height / 2f, width, height / 2f, drawStartColor, drawEndColor, Shader.TileMode.CLAMP)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        // 绘制背景
        rect.set(0f, 0f, width.toFloat(), height.toFloat())
        paint.shader = backgroundShader
        canvas.drawRoundRect(rect, radius, radius, paint)
        // 绘制进度条
        rect.set(0f, 0f, width * progress, height.toFloat())
        paint.shader = createLinearGradient(width * progress, progressStartColor, progressEndColor, progressColor)
        canvas.drawRoundRect(rect, radius, radius, paint)
    }

}